package com.china08.yxyapi.api.manage.sch.platform.live.lesson.down;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.live.YxyLiveLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.live.YxyLiveLessonItemRepository;

@CrossOrigin
@RestController
public class LiveSchLessonSetController extends YxyApiBaseController {

    @Autowired
    private YxyLiveLessonItemRepository yxyLiveLessonItemRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/live/lesson/down/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        YxyLiveLessonItem yxyLiveLesson = yxyLiveLessonItemRepository.findById(reqModel.id);

        // 关闭
        if (reqModel.status == 0) {
            yxyLiveLesson.setRecordOpen(2);
        } else {
            yxyLiveLesson.setRecordOpen(1);
        }
        yxyLiveLessonItemRepository.save(yxyLiveLesson);
        RespModel model = new RespModel();
        model.setMessage("设置成功");

        return model;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String id;
        public Integer status;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(id), "课时ID为空");
        }
    }

    public static class RespModel extends YxyApiRespModel {
    }

}
